using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;

public class StreamTools
{
	public static IEnumerator LoadBytes<T>(string filePath, Delegate1Args<T> onloaded) where T : new()
	{
		WWW www = new WWW(filePath);
		yield return www;
		if (string.IsNullOrEmpty(www.error))
		{
			onloaded(DeserializeObject<T>(www.bytes));
			yield break;
		}
		UnityEngine.Debug.Log(www.error);
		onloaded(default(T));
	}

	public static FileInfo[] GetAllFile(string path, string pattern = null)
	{
		DirectoryInfo directoryInfo = new DirectoryInfo(path);
		List<FileInfo> list = new List<FileInfo>();
		if (directoryInfo.Exists)
		{
			FileInfo[] array = (!string.IsNullOrEmpty(pattern)) ? directoryInfo.GetFiles(pattern) : directoryInfo.GetFiles();
			FileInfo[] array2 = array;
			foreach (FileInfo fileInfo in array2)
			{
				string name = fileInfo.Name;
				if (name.Substring(name.LastIndexOf(".") + 1).CompareTo("meta") != 0)
				{
					list.Add(fileInfo);
				}
			}
			return list.ToArray();
		}
		return null;
	}

	public static byte[] SerializeObject(object o)
	{
		using (MemoryStream memoryStream = new MemoryStream())
		{
			BinaryFormatter binaryFormatter = new BinaryFormatter();
			binaryFormatter.Serialize(memoryStream, o);
			return memoryStream.ToArray();
		}
	}

	public static void SerializeObject(object o, string fileName)
	{
		string path = fileName.Substring(0, fileName.LastIndexOf('/'));
		if (!Directory.Exists(path))
		{
			Directory.CreateDirectory(path);
		}
		using (FileStream serializationStream = new FileStream(fileName, FileMode.Create))
		{
			BinaryFormatter binaryFormatter = new BinaryFormatter();
			try
			{
				binaryFormatter.Serialize(serializationStream, o);
			}
			catch (NotImplementedException exception)
			{
				UnityEngine.Debug.LogException(exception);
			}
		}
	}

	public static T DeserializeObject<T>(byte[] buffer) where T : new()
	{
		T val = new T();
		try
		{
			using (MemoryStream serializationStream = new MemoryStream(buffer))
			{
				BinaryFormatter binaryFormatter = new BinaryFormatter();
				return (T)binaryFormatter.Deserialize(serializationStream);
			}
		}
		catch (Exception ex)
		{
			UnityEngine.Debug.Log(ex.Message);
			return default(T);
		}
	}

	public static T DeserializeObject<T>(string fileName) where T : new()
	{
		T result = new T();
		try
		{
			using (FileStream fileStream = new FileStream(fileName, FileMode.Open))
			{
				byte[] array = new byte[fileStream.Length];
				if (fileStream.Read(array, 0, array.Length) != 0)
				{
					using (MemoryStream serializationStream = new MemoryStream(array))
					{
						BinaryFormatter binaryFormatter = new BinaryFormatter();
						return (T)binaryFormatter.Deserialize(serializationStream);
					}
				}
				UnityEngine.Debug.LogError("File : " + fileName + " content is null");
				return result;
			}
		}
		catch (Exception ex)
		{
			UnityEngine.Debug.Log(ex.Message);
			return default(T);
		}
	}

	public static string GetStreamingAssetsPath(bool usePrefix = false)
	{
		string str = (!usePrefix) ? string.Empty : "jar:file://";
		return str + Application.dataPath + "!/assets/";
	}

	public static string GetPersistentDataPath()
	{
		return Application.persistentDataPath + "//";
	}

	public static string GetStreamingAssetsPathInEditor()
	{
		return "Assets/StreamingAssets/";
	}

	public static T Clone<T>(T t) where T : new()
	{
		using (MemoryStream memoryStream = new MemoryStream())
		{
			BinaryFormatter binaryFormatter = new BinaryFormatter();
			binaryFormatter.Serialize(memoryStream, t);
			memoryStream.Seek(0L, SeekOrigin.Begin);
			return (T)binaryFormatter.Deserialize(memoryStream);
		}
	}

	public static void CreateFold(string path, bool deleteIfExist = false)
	{
		if (!Directory.Exists(path))
		{
			Directory.CreateDirectory(path);
		}
		else if (deleteIfExist)
		{
			Directory.Delete(path, recursive: true);
			Directory.CreateDirectory(path);
		}
	}
}
